package com.buddies.lottery;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.blankj.utilcode.util.Utils;
import com.buddies.lottery.common.ConfigManager;
import com.buddies.lottery.common.UrlConstants;
import com.buddies.lottery.data.RetrofitManager;
import com.buddies.lottery.utils.ACache;
import com.buddies.lottery.utils.AppUtils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.stetho.Stetho;
import com.fm.openinstall.OpenInstall;
import com.google.gson.Gson;
import com.r0adkll.slidr.model.SlidrConfig;
import com.r0adkll.slidr.model.SlidrPosition;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.BuglyStrategy;
import com.tencent.bugly.crashreport.BuglyLog;

public class App extends Application {

    private static final String TAG = App.class.getSimpleName();

    public static boolean LOGGABLE = true;

    private static App mInstance;
    private static Gson mGson;

    // 客户端和服务器之间的时间差
    private long delta;

    private ACache mACache;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        String channel = AppUtils.getChannel();
        LOGGABLE = BuildConfig.DEBUG || "dev".equals(channel);

        Utils.init(this);
        Fresco.initialize(this);
        Stetho.initializeWithDefaults(this);

        initRetrofit();

        // Bugly
        BuglyStrategy strategy = new BuglyStrategy();
        strategy.setAppChannel(channel);
        strategy.setBuglyLogUpload(true);
        strategy.setEnableANRCrashMonitor(true);
        strategy.setEnableNativeCrashMonitor(true);
        strategy.setDeviceID(AppUtils.getDeviceID());
        Bugly.init(getApplicationContext(), "de2192d2e0", LOGGABLE, strategy);
        BuglyLog.d(TAG, "onCreate");

        if (ConfigManager.getInstance().isLogin()) {
            Bugly.setUserId(App.getInstance().getContext(), ConfigManager.getInstance().getUserId());
        }

        OpenInstall.init(this);
    }

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        MultiDex.install(this);
    }

    public static App getInstance() {
        return mInstance;
    }

    public Context getContext() {
        return mInstance.getApplicationContext();
    }

    public static synchronized Gson getGson() {
        if (mGson == null) {
            mGson = new Gson();
        }
        return mGson;
    }

    public static SlidrConfig getSlidrConfig() {
        return new SlidrConfig.Builder()
                .position(SlidrPosition.LEFT)
                .sensitivity(0.2f)
                .velocityThreshold(2400)
                .distanceThreshold(0.25f)
                .edge(true)
                .build();
    }

    public void initRetrofit() {
        RetrofitManager.getInstance().init(UrlConstants.BASE_URL, UrlConstants.SALT);
    }

    public boolean isDebug() {
        return LOGGABLE;
    }

    public void updateDeltaByServerTime(long serverTimestamp) {
        delta = System.currentTimeMillis() - serverTimestamp;
    }

    public long currentTimeMillis() {
        return System.currentTimeMillis() + delta;
    }

    public ACache getACache() {
        if (mACache == null) {
            try {
                mACache = ACache.get(this);
            } catch (Throwable e) {
                mACache = ACache.get(getCacheDir());
            }
        }
        return mACache;
    }

}
